package com.practice.concurrency.lock;

public class UnfairWaitLock {
	
	private boolean isLocked = false;
	
	private Thread lockingThread = null;
	
	public synchronized void lock() throws InterruptedException {
		while (isLocked) {
			wait();
		}
		isLocked = true;
		lockingThread = Thread.currentThread();
	}
	
	public synchronized void unlock() {
		if (this.lockingThread != Thread.currentThread()) {
			throw new IllegalMonitorStateException(
					"Calling thread has not locked this lock");
		}
		isLocked = false;
		lockingThread = null;
		notify();
	}
}

class Synchronizer {
	UnfairWaitLock lock = new UnfairWaitLock();
	public void doSynchronized() throws InterruptedException {
		this.lock.lock();
		//critical section, do a lot of work which takes a long time
		this.lock.unlock();
	}
}
